<?php

class edge {

	const STATE_STOPPED = 1;
	const STATE_BUFFERING = 2;
	const STATE_PLAYING = 4;
	const STATE_CONNECTING = 8;

	static $size = array(
		'aac24' => '24000'
	);

	static $type = array(
		'aac24' => 'AAC+ 24kbps'
	);
	
	static $list = array(
		'aac24' => array(
			'di.fm Chillout' => 'http://www.di.fm/aacplus/chillout.pls',
			'di.fm Lounge' => 'http://www.di.fm/aacplus/lounge.pls',
			'di.fm Vocal Trance' => 'http://www.di.fm/aacplus/vocaltrance.pls'
		)
	);

	static $w;
	static $t;

	static $title = 'edgecast Radio Player';
	static $state = self::STATE_STOPPED;
	
	static $config = '.edgecast';
	static $inttype = 0;
	static $intstream = 0;
	static $logtitle = true;
	static $logfile = 'edgecast-\$s-playlog.txt';
	static $cachesize = 1024;
	static $cachefill = 75;
	static $usetray = true;
	static $songtraypop = true;
	
	static $buffermax = 1;
	static $bufferpre = 0;
	static $bufferpremax = 0;
	static $bufferlowthresh = 0.3;
	
	static $deskw;
	static $deskh;	
	
	static $inittime = 0;
	static $starttime = 0;
	static $distance = 0;

	static $lasttime = 0;
	
	static $currenttitle;
	static $lasttitle;
	static $titlebuffer = array();
	
	static $stype;
	static $stream;
	static $url;
	
	static function queue_title($title) {

		$future = (time() + edge::$distance + (edge::$bufferpre / edge::$size[edge::$stype]));	

		array_pop(edge::$titlebuffer);
		edge::$titlebuffer[$future] = $title;
		krsort(edge::$titlebuffer);
		
		if(edge::$logtitle) {
			$fp = fopen(edge::mkfilesafe(edge::$logfile),'a');
			fwrite($fp,sprintf("%s %s\n",date('Y/m/d H:i:s'),$title));
			fclose($fp);
		}
		return;
	}

	static function get_title() {
		$now = time();

		// unless di fm just offsets there titles weird i have no idea why
		// i had to add this twice to make the title updates almost line up.
		// (it was first added on title entry addition)

		$timeleft = edge::$bufferpre / edge::$size[edge::$stype];

		foreach(edge::$titlebuffer as $time => $title) {
			if(($time + $timeleft) <= $now) {
				return $title;
			}
		}
		
		return;
	}
	
	static function mkfilesafe(&$data) {
		$newdata = $data;
		$newdata = str_replace('$s',edge::$stream,$newdata);
		$newdata = str_replace(' ','-',$newdata);
//		$newdata = str_replace('.','',$newdata);	
		return strtolower($newdata);
	}
	
	static function clear_title_buffer() {
		self::$titlebuffer = array(
			9=>'',8=>'',7=>'',6=>'',5=>'',
			4=>'',3=>'',2=>'',1=>'',0=>''
		); return;	
	}
	
	static function update_ui() {
		while(Gtk::events_pending() || Gdk::events_pending()) {
			Gtk::main_iteration_do(true);
		} return;
	}
	
	static function active() {
		if(self::$state != self::STATE_STOPPED) {
			return true;
		} else {
			return false;
		}
	}

	static function idle() {
		if(self::$state != self::STATE_STOPPED) {
			return false;
		} else {
			return true;
		}
	}
	
	static function read_config() {
		$fp = fopen(self::$config,'r');

		self::$list = array();
		self::$type = array();
		self::$size = array();		
		
		while($dump = trim(fgets($fp))) {
			if(preg_match('/^(.+?)="(.+?)"/',$dump,$match)) {
				switch($match[1]) {
					case('type'): {
						list($key,$label,$size) = explode('|',$match[2]);
						self::$type[$key] = $label;
						self::$size[$key] = $size;
						self::$list[$key] = array();
						break;
					}
					case('stream'): {	
						list($type,$label,$url) = explode('|',$match[2]);
						self::$list[$type][$label] = $url;
						break;
					}
					case('lasttype'): {
						self::$inttype = (int)$match[2];
						break;
					}
					case('laststream'): {
						self::$intstream = (int)$match[2];
						break;
					}
					case('cachesize'): {
						self::$cachesize = (int)$match[2];
						break;
					}
					case('cachefill'): {
						self::$cachefill = (float)$match[2];
						break;
					}
					case('bufferlowthresh'): {
						self::$bufferlowthresh = (float)$match[2];
						break;
					}
					case('usetray'): {
						self::$usetray = (boolean)$match[2];
						break;
					}
					case('songtraypop'): {
						self::$songtraypop = (boolean)$match[2];
						break;
					}
					case('logtitle'): {
						self::$logtitle = (boolean)$match[2];
						break;
					}
					case('logfile'): {
						self::$logtitle = $match[2];
						break;
					}
				}
			}
		} fclose($fp);
	}
	
	static function write_config() {
		$fp = fopen(self::$config,'w');
		
		fwrite($fp,sprintf("lasttype=\"%d\"\n",self::$inttype));
		fwrite($fp,sprintf("laststream=\"%d\"\n",self::$intstream));
		fwrite($fp,sprintf("logtitle=\"%d\"\n",self::$logtitle));
		fwrite($fp,sprintf("logfile=\"%s\"\n",self::$logfile));
		fwrite($fp,sprintf("cachesize=\"%d\"\n",self::$cachesize));
		fwrite($fp,sprintf("cachefill=\"%.1f\"\n",self::$cachefill));
		fwrite($fp,sprintf("bufferlowthresh=\"%.1f\"\n",self::$bufferlowthresh));
		fwrite($fp,sprintf("usetray=\"%d\"\n",self::$usetray));
		fwrite($fp,sprintf("songtraypop=\"%d\"\n",self::$songtraypop));
		
		foreach(self::$type as $key => $label) {
			fwrite($fp,sprintf(
				"type=\"%s|%s|%s\"\n",
				$key,
				$label,
				self::$size[$key]
			));
		}		

		foreach(self::$list as $type => $list) {
			foreach($list as $label => $url) {
				fwrite($fp,sprintf(
					"stream=\"%s|%s|%s\"\n",
					$type,
					$label,
					$url
				));				
			}
		}

		fclose($fp);
	}
}

?>